/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018-01-6 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.admin.controller.ware;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.admin.controller.GenericController;
import com.itttq.gshop.domain.po.WareCategoryPo;
import com.itttq.gshop.domain.to.PageParamTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.connector.IWareCategoryConnector;

@RestController
@RequestMapping("ware/category")
public class WareCategoryController extends GenericController {
	private static final long serialVersionUID = 362159244139514786L;
	@Autowired
	private IWareCategoryConnector wareCategoryConnector;
	
	/**
	 * 根据条件查询WareCategoryPo对象，并分页
	 * 
	 * @param wareCategory
	 * @param request
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("categorys.html")
	public ModelAndView getWareCategorys(WareCategoryPo wareCategory, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/ware/categorys");
		PageParamTo ppt = null == request ? new PageParamTo() : getPageParam(request);
		wareCategory.setStart(ppt.getStart());
		wareCategory.setSize(ppt.getSize());
		ResultPageTo<WareCategoryPo> rpt = wareCategoryConnector.getWareCategorysByPage(wareCategory);
		this.getPageMv("categorys", mv, rpt);
		getAllCategories(mv);
		mv.addObject("category", wareCategory);
		return mv;
	}
	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping("add.html")
	public ModelAndView addWareCategory() {
		ModelAndView mv = new ModelAndView("/ware/categoryAdd");
		getAllCategories(mv);
		return mv;
	}
	/**
	 * 跳转到查看页面
	 * @param id
	 * @return
	 */
	@RequestMapping("view/{id}.html")
	public ModelAndView viewWareCategory(@PathVariable("id") Integer id) {
		ModelAndView mv = addWareCategory();
		if(id != null) {
			mv.addObject("category", wareCategoryConnector.getWareCategory(id));
		}
		mv.addObject("op", "view");
		return mv;
	}
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("edit/{id}.html")
	public ModelAndView editWareCategory(@PathVariable("id") Integer id) {
		ModelAndView mv = viewWareCategory(id);
		mv.addObject("op", "edit");
		return mv;
	}
	
	/**
	 * 保存WareCategoryPo对象，无ID则新增，有ID则更新
	 * 
	 * @param wareCategory
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("save.html")
	public ModelAndView saveWareCategory(WareCategoryPo wareCategory) {
		wareCategoryConnector.saveWareCategory(wareCategory);
		return getWareCategorys(new WareCategoryPo(), null);
	}

	/**
	 * 根据ID删除WareCategoryPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("del/{id}.html")
	@ResponseBody
	public ModelAndView delWareCategory(@PathVariable("id") Integer id) {
		if(id != null) {
			wareCategoryConnector.delWareCategory(id);
		}
		return getWareCategorys(new WareCategoryPo(), null);
	}
}